WordApplication Class

Used to automate Microsoft Word.

Events

None

Properties

None

Methods

None

More information available in parent classes: OLEObject:Object


Notes

The language that you use to automate Microsoft Office applications is documented by Microsoft and numerous third-party books on Visual Basic for Applications (VBA). Microsoft Office applications provide online help for VBA. To access the online help, choose Macros from the Tools Menu of your MS Office application, and then choose Visual Basic Editor from the Macros submenu. When the Visual Basic editor appears, choose Microsoft Visual Basic Help from the Help menu. The help is contextual in the sense that it provides information on automating the Office application from which you launched the Visual Basic editor.

If VBA Help does not appear, you will need to install the help files. On Windows Office 2003, Office prompts you to install the VBA help files when you first request VBA help. You don't need the master CD. On Macintosh, Office v.X does not install the VBA help files as part of the full install. Quit out of Office and locate your master CD. Open the "Value Pack" folder and double-click the Value Pack installer. In the Value Pack installer dialog, scroll down to the Programmability topic, select it, and click Continue. The installer will then add the VBA help files and examples to your Office installation. When the install finishes, the VBA help files will be available to the Visual Basic editor within all your Office X applications.

Microsoft has additional information on VBA at http://msdn.microsoft.com/vbasic/ and have published their own language references on VBA. One of several third-party books on VBA is "VB & VBA in a Nutshell: The Language" by Paul Lomax (ISBN: 1-56592-358-8).


Example

This example creates a Word document from the text entered into and EditField on the form, EditField1. The code is in a PushButton's Action event handler. It also sets the font and font size of the text.

Dim word as WordApplication
Dim zdoc as WordDocument
Dim style as WordStyle
Dim v as Variant
  
word = New WordApplication
doc = word.documents.add
doc.range.text = Editfield1.text

v = doc.paragraphs.item(1).style
If v.objectvalue IsA wordstyle then
 style = wordstyle(v.objectvalue)
 styletext.text = style.namelocal
 stylefontname.text = style.font.name
 stylefontsize.text = Str(style.font.size)
end if
  
Exception err as OLEException
  MsgBox err.message

This example does a search and replace operation on the text in EditField1, using the text in the EditFields, FindText and ReplaceText. The Word document is updated after the search and replace.

Dim word as WordApplication
Dim doc as WordDocument
Dim s as String
Dim r as Boolean
  
Const wdFindContinue = 1
Const wdReplaceAll = 2
  
word = New WordApplication
doc = word.activedocument

s = ReplaceAll(Editfield1.text, FindText.text, ReplaceText.text)
  
#if TargetWin32 then
 doc.range.text = s
#else
 r =word.selection.find.execute(FindText.text, False, False, False, False, 0, True, _
           wdFindContinue, False, ReplaceText.text, wdReplaceAll)
#endif

Editfield1.text = doc.range.text

Exception err as OLEException
  MsgBox err.message

See Also

ExcelApplication, Office, OLEObject, OLEException, PowerPointApplication classes.